package com.mef.smsplat.service;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hc24.util.StringUtil;
import com.mef.smsplat.dao.OperatorInfoDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.EntityDao;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.OperatorInfoDomain;
import com.mef.smsplat.service.base.BaseService;
import com.mef.smsplat.util.Constants;

@Service
@Transactional
public class OperatorInfoService extends BaseService<OperatorInfoDomain, String> {
	
	private OperatorInfoDao operatorInfoDao;
	
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setOperatorInfoDao(OperatorInfoDao operatorInfoDao) {
		this.operatorInfoDao = operatorInfoDao;
	}

	@Override
	public EntityDao getEntityDao() {
		return this.operatorInfoDao;
	}
	
	/**
	 * 登录
	 * @param username
	 * @param password
	 * @return
	 */
	public OperatorInfoDomain login(String username,String password){
		OperatorInfoDomain operator = operatorInfoDao.query(username, password);
		//这里记录最后登陆时间业务逻辑处理
		if(operator != null){
			operator.setLoginTime(new Date());
			save(operator);
		}
		return operator;
	}
	
	/**
	 * 查询账号是否唯一
	 * @param name
	 * @param id
	 * @return
	 */
	@Transactional(readOnly=true)
	public boolean isUnique(String name, String id){
		boolean isUnique = operatorInfoDao.queryByNameAndId(name, id);
		return isUnique;
	}
	
	/**
	 * 分页查询
	 * @param username
	 * @param dgm
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page<OperatorInfoDomain> query(String operatorName,DataGridModel dgm){
		return operatorInfoDao.query(operatorName, dgm);
	}
	
	/**
	 * 重置密码
	 */
	public void resetPassword(String[] ids){
		//循环查询对象，并且执行重置密码操作
		for (int i = 0; i < ids.length; i++) {
			OperatorInfoDomain operator = operatorInfoDao.getById(ids[i]);
			if(operator!=null){
				operator.setOperatorPassword(StringUtil.md5(Constants.PASSWORD));
				operatorInfoDao.update(operator);
			}
		}
	}
	/**
	 * 启用
	 */
	public void enable(String[] ids){
		//循环查询对象，并且执行重置密码操作
		for (int i = 0; i < ids.length; i++) {
			OperatorInfoDomain operator = operatorInfoDao.getById(ids[i]);
			if(operator!=null){
				operator.setStatus(Constants.NORMAL);
				operatorInfoDao.update(operator);
			}
		}
	}
	/**
	 * 停用
	 */
	public void disable(String[] ids){
		//循环查询对象，并且执行重置密码操作
		for (int i = 0; i < ids.length; i++) {
			OperatorInfoDomain operator = operatorInfoDao.getById(ids[i]);
			if(operator!=null){
				operator.setStatus(Constants.STOP);
				operatorInfoDao.update(operator);
			}
		}
	}
}
